package com.example.xianzhi.dao.impl;

import com.example.xianzhi.dao.UserDao;
import com.example.xianzhi.entity.TbUser;
import com.example.xianzhi.util.ORMUtil;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public TbUser selectUser(int user_id) {
        String sql=" SELECT * FROM tb_user WHERE user_id=?";
        List<TbUser> tbUsers = new ORMUtil().selectList(TbUser.class, sql, user_id);


        return tbUsers.size()>0 ? tbUsers.get(0) : null;
    }

    @Override
    public List<TbUser> selectuserss(String wx_nick, String wx_address, int user_credit) {
        String sql ="SELECT * from  tb_user where 1=1 ";

        List<Object> params = new ArrayList<>();



        if(wx_nick != null && !"".equals(wx_nick)){

            sql += " and wx_nick like ?  ";
            params.add("%"+wx_nick+"%");
        }
        if(wx_address != null && !"".equals(wx_address)){

            sql += " and wx_address like ?  ";
            params.add("%"+wx_address+"%");
        }
        if(user_credit != 0 ){

            sql += " and user_credit >= ? ";

            params.add(user_credit);
        }


        Object[] objects = params.toArray();


        return new ORMUtil().selectList(TbUser.class,sql,objects);
    }

    @Override
    public TbUser selectUser(String wx_open_id) {
        System.out.println(">>>>>>>进入SELECT语句");
        String sql="SELECT * FROM tb_user WHERE wx_open_id=?";
        List<TbUser> tbGoods = new ORMUtil().selectList(TbUser.class, sql, wx_open_id);

        return tbGoods.size()>0 ? tbGoods.get(0) : null;
    }

    @Override
    public void insertUser(String wx_open_id, String wx_nick, String wx_head, String wx_address, String user_num, double user_credit, int user_money, String uesr_add) {
        System.out.println(">>>>>>>>进入insert语句");
        String sql="insert into tb_user(wx_open_id, wx_nick,wx_head,wx_address,user_num,user_money,user_credit,uesr_add)  values(?, ?, ?,?,?,?,?,?)";
        new ORMUtil().executeDML(sql,wx_open_id,wx_nick,wx_head,wx_address,user_num,user_credit,user_money,uesr_add);

    }


}
